﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using mfe.bll;

namespace mfe_proj.Forms
{
    public delegate void MagazineTOCInfoEventHandler(object sender, MagazineTOCInfoEventArgs e);

    public partial class MagazineTOCInfo : Form
    {
        public event MagazineTOCInfoEventHandler TocAdded;
        MagazineTOC toc = null;
        bool _IsNew=true;
        public bool IsNew { get { return _IsNew; } set { _IsNew = value; } }
        string _ItemGuidID;
        public string ItemGuidID { get { return _ItemGuidID; } set { _ItemGuidID = value; } }

        public MagazineTOCInfo()
        {
            InitializeComponent();
        }

        bool SaveData()
        {
            bool retval = false;
            if (IsNew)
                toc = new MagazineTOC();
            toc.ItemGuidID = _ItemGuidID;
            toc.Title = txtTitle.Text.Trim();
            toc.Author = txtAuthor.Text.Trim();
            toc.Page = (int)intPage.Value;
            toc.SortOrder = (int)intSortOrder.Value;

            if (_IsNew)
            {
                retval = toc.Save();
                if (TocAdded != null) TocAdded(this, new MagazineTOCInfoEventArgs(toc));
            }
            else
            {
                retval = toc.Update();
            }
            return retval;
        }

        private void MagazineTOCInfo_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape) Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (SaveData()) Close();
        }
    }

    public class MagazineTOCInfoEventArgs : EventArgs
    {
        MagazineTOC _TOC;
        public MagazineTOC TOC { get { return _TOC; } set { _TOC = value; } }
        public MagazineTOCInfoEventArgs(MagazineTOC toc)
        {
            _TOC = toc;
        }
    }
}
